<?php

class Page implements InterfacePage {
    protected $style,
              $theme_path = "core/themes/",
              $theme,
              $title,
              $header,
              $central,
              $menu,
              $footer,
              $logo;

    function __construct($data) {
        if (is_array($data)) {
            $this->title = $data['name'];
            $this->header = $data['head_text'];
            $this->footer = $data['footer_text'];
            $this->setTheme($data['theme']);
            $this->logo = $data['logo_path'];
        }
    }

    function header($text) {
        $this->header = $text;
    }

    function menu($data) {
        $resultStr = '';
        if (is_array($data)) {
            $resultStr .= "<ul>";
            foreach ($data as $v) {
                $resultStr .= "<li><a href='" .$_SERVER['PHP_SELF']
                           . "?cat={$v['id']}' title='{$v['description']}'>{$v['name']}</a></li>";
            }
            $resultStr .= "</ul>";
        }
        $this->menu = $resultStr;
    }

    function central($data,$param=null) {
        $resultStr = '';
        $strLength = 180;
        if (is_array($param)) {
            $array = $param;
            $strLength = null;
        } else {
            $array = $data;
        }

        foreach ($array as $v) {
            $resultStr .= "<div class='news-part'>";

            $resultStr .= mb_substr($v['text'],0,$strLength,"utf-8") . "&nbsp;";

            if ($strLength) $resultStr .= "<a href='".$_SERVER['PHP_SELF']."?page={$v['id']}'>Далее..</a>";
            else $resultStr .= "<a href='".$_SERVER['PHP_SELF']."'>Назад</a>";

            $resultStr .= "</div>";
        }

        $this->central = $resultStr;
    }

    function footer($text) {
        $this->footer = $text;
    }

    function setTheme($name) {
        $css_name = "";
        $theme_dir = scandir($this->theme_path);
        foreach ($theme_dir as $v) {
            $v = strtolower($v);
            if ($name == $v) {
                $css_name = $v;
                break;
            } else {
                $css_name = "default";
            }
        }

        $this->style = $this->theme_path . $css_name . "/css/style.css";
    }


    function initPage() {
        include_once __DIR__.'\..\includes\page.php';
    }

    function drawTheme() {
        $theme_dir = scandir($this->theme_path);
        $txt = '';
        foreach($theme_dir as $v) {
            if (is_file($v) || ($v == '..') || ($v == '.')) continue;
            $txt .= "<a href='' class='$v'>$v</a>";
        }
        return $txt;
    }
}